import type { Word, GameSettings, GameStats, CategoryStats, Pet, Achievement, UserProgress } from '@/types/vocabulary';
import { defaultVocabulary } from '@/data/vocabulary';

const VOCABULARY_KEY = 'wordmaster_vocabulary';
const SETTINGS_KEY = 'wordmaster_settings';
const GAME_STATS_KEY = 'wordmaster_game_stats';
const CATEGORY_STATS_KEY = 'wordmaster_category_stats';
const PETS_KEY = 'wordmaster_pets';
const ACHIEVEMENTS_KEY = 'wordmaster_achievements';
const USER_PROGRESS_KEY = 'wordmaster_user_progress';

export const loadVocabulary = (): Word[] => {
  try {
    const stored = localStorage.getItem(VOCABULARY_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading vocabulary:', error);
  }
  return defaultVocabulary;
};

export const saveVocabulary = (vocabulary: Word[]): void => {
  try {
    localStorage.setItem(VOCABULARY_KEY, JSON.stringify(vocabulary));
  } catch (error) {
    console.error('Error saving vocabulary:', error);
  }
};

export const loadSettings = (): GameSettings => {
  try {
    const stored = localStorage.getItem(SETTINGS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading settings:', error);
  }
  return {
    difficulty: 'medium',
    timeLimit: 15,
    soundEnabled: true,
  };
};

export const saveSettings = (settings: GameSettings): void => {
  try {
    localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
  } catch (error) {
    console.error('Error saving settings:', error);
  }
};

export const loadGameStats = (): GameStats => {
  try {
    const stored = localStorage.getItem(GAME_STATS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading game stats:', error);
  }
  return {
    matching: {
      totalGames: 0,
      totalTime: 0,
      averageTime: 0,
      bestTime: 0,
    },
    spelling: {
      totalGames: 0,
      totalWords: 0,
      correctWords: 0,
      accuracy: 0,
    },
    quiz: {
      totalGames: 0,
      totalScore: 0,
      averageScore: 0,
      bestScore: 0,
      accuracy: 0,
    },
  };
};

export const saveGameStats = (stats: GameStats): void => {
  try {
    localStorage.setItem(GAME_STATS_KEY, JSON.stringify(stats));
  } catch (error) {
    console.error('Error saving game stats:', error);
  }
};

export const loadCategoryStats = (): CategoryStats => {
  try {
    const stored = localStorage.getItem(CATEGORY_STATS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading category stats:', error);
  }
  return {};
};

export const saveCategoryStats = (stats: CategoryStats): void => {
  try {
    localStorage.setItem(CATEGORY_STATS_KEY, JSON.stringify(stats));
  } catch (error) {
    console.error('Error saving category stats:', error);
  }
};

export const loadPets = (): Pet[] | null => {
  try {
    const stored = localStorage.getItem(PETS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading pets:', error);
  }
  return null;
};

export const savePets = (pets: Pet[]): void => {
  try {
    localStorage.setItem(PETS_KEY, JSON.stringify(pets));
  } catch (error) {
    console.error('Error saving pets:', error);
  }
};

export const loadAchievements = (): Achievement[] | null => {
  try {
    const stored = localStorage.getItem(ACHIEVEMENTS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading achievements:', error);
  }
  return null;
};

export const saveAchievements = (achievements: Achievement[]): void => {
  try {
    localStorage.setItem(ACHIEVEMENTS_KEY, JSON.stringify(achievements));
  } catch (error) {
    console.error('Error saving achievements:', error);
  }
};

export const loadUserProgress = (): UserProgress | null => {
  try {
    const stored = localStorage.getItem(USER_PROGRESS_KEY);
    if (stored) {
      return JSON.parse(stored);
    }
  } catch (error) {
    console.error('Error loading user progress:', error);
  }
  return null;
};

export const saveUserProgress = (progress: UserProgress): void => {
  try {
    localStorage.setItem(USER_PROGRESS_KEY, JSON.stringify(progress));
  } catch (error) {
    console.error('Error saving user progress:', error);
  }
};
